package graph.model;

import graph.config.Config;

import java.util.ArrayList;


/**
 * @author ron
 * 
 */
public class Vertex {
	private String name;
	private ArrayList<Edge> edges;
	private ArrayList<Vertex> predecessors;

	private int shortest;
	private int longest;

	public Vertex( String name ) {
		this.name = name;
		edges = new ArrayList<Edge>();
		predecessors = new ArrayList<Vertex>();
	}
	
	/**
	 * Check if this is the first Vertex of a graph
	 * @return is root Vertex
	 */
	public boolean isFirst() {
		return predecessors.size() == 0;
	}
	
	/**
	 * Check if this is the last Vertex of a graph
	 * @return is last Vertex
	 */	public boolean isLast() {
		 return edges.size() == 0;
	 }
	 
	 /**
	  * A function to get the weight of the Edge beloning to the given Vertex
	  * @param end
	  * @return the weight of the Edge
	  */
	 public int getEdgeWeightFor( Vertex end ) {
		 for( Edge edge : edges ) {
			 if( edge.getEnd().equals( end ) ) {
				 return edge.getWeight();
			 }
		 }
		 return 0;
	 }

	/**
	 * Adds a predecessor to the current Vertex 
	 */
	public void addPredecessor( Vertex vertex ) {
		if( Config.debug )
			System.out.println( "Added predecessor " + vertex.getName() + " for " + name );
		predecessors.add( vertex );
	}

	/**
	 * Prints a nice overview of this Vertex's stats
	 */
	public void print() {
		System.out.println( "Shortest path: " + getShortest() );
		System.out.println( "Longest path: " + longest );
		System.out.println( "Predecossors:" );
		if( predecessors.size() == 0 ) {
			System.out.println( "None" );
			return;
		}
		for( Vertex v : predecessors ) {
			System.out.println( "- " + v.getName() );
		}
	}

	/**
	 * @return
	 */
	public ArrayList<Vertex> getPredecessors() {
		return predecessors;
	}

	/**
	 * @param edge
	 */
	public void addEdge( Edge edge ) {
		edges.add( edge );
	}

	/**
	 * @return
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return
	 */
	public ArrayList<Edge> getEdges() {
		return edges;
	}

	/**
	 * @return
	 */
	public int getShortest() {
		return shortest;
	}

	/**
	 * @param shortest
	 */
	public void setShortest( int shortest ) {
		this.shortest = shortest;
	}

	/** 
	 * @return
	 */
	public int getLongest() {
		return longest;
	}

	/**
	 * @param longest
	 */
	public void setLongest( int longest ) {
		this.longest = longest;
	}
}
